/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import datastorage.RMSManager;

/**
 *
 * @author Kvest
 */
public class Settings 
{
    public static final String JAD = ".jad";
    public static final String JAR = ".jar";
    public static final int FS_SMALL = 0;
    public static final int FS_MIDDLE = 1;
    public static final int FS_BIG = 2;
    
    //private static final String DEFAULT_HOST = "78.29.13.82:3426"; /* "79.134.31.199:3426";// "109.201.223.43:3426";  "109.120.140.93:3426"*/;
    private static final String DEFAULT_HOST = "192.168.1.121:3426";//78.29.13.82:3426"; /* "79.134.31.199:3426";// "109.201.223.43:3426";  "109.120.140.93:3426"*/;
    private static final String DEFAULT_SECOND_HOST = "78.29.13.82:3426";
    //private static final String DEFAULT_SECOND_HOST = "79.134.18.222:3426";
    
    private static final String RMS_NAME = "settings";
    private static final String HOST_KEY = "host";
    private static final String LOGIN_KEY = "login";
    private static final String PASSWORD_KEY = "pass";
    private static final String SAVE_DATA_KEY = "save_data";
    private static final String NEW_ORDER_MELODY_KEY = "new_ord_mel";
    private static final String START_ORDER_MELODY_KEY = "start_ord_mel";
    private static final String CANCEL_ORDER_MELODY_KEY = "cancel_ord_mel";
    private static final String CONNECTION_ERROR_MELODY_KEY = "con_err_mel";
    private static final String MESSAGE_MELODY_KEY = "message_mel";
    private static final String UPDATE_TYPE_KEY = "update_type";
    private static final String FONT_SIZE_KEY = "font_size";
    private static final String SECOND_HOST_KEY = "host";

    private static final int DEFAULT_MASK = 0xffff;

    private static Settings settings = null;
    private static final Object lock = new Object();
    
    private RMSManager rmsManager;

    private String host;
    private String secondHost;
    private String login;
    private String password;
    private boolean saveLoginPass;
    private int functionsMask;
    
    private int newOrderMelodyId;
    private int startOrderMelodyId;
    private int cancelOrderMelodyId;
    private int connectionErrorMelodyId;
    private int messageMelodyId;
    
    private String updateType;
    private int fontSize;
    
    public static Settings getSettings()
    {
        synchronized(lock)
        {
            if (settings == null)
            {
                settings = new Settings();
            }
        }
        
        return settings;
    }
    
    private Settings()
    {
        rmsManager = new RMSManager(RMS_NAME);

        host = DEFAULT_HOST;
        secondHost = DEFAULT_SECOND_HOST;
        login = "";
        password = "";
        saveLoginPass = true;
        functionsMask = DEFAULT_MASK;
        
        newOrderMelodyId = 0;
        startOrderMelodyId = 1;
        cancelOrderMelodyId = 2;
        connectionErrorMelodyId = 3;
        messageMelodyId = 4;
        
        updateType = JAD;
        fontSize = FS_SMALL;
        
        load();
    }
    
    public final void load()
    {
        if (rmsManager.containsKey(HOST_KEY))
        {
            host = rmsManager.get(HOST_KEY);
        }
        if (rmsManager.containsKey(SECOND_HOST_KEY))
        {
            secondHost = rmsManager.get(SECOND_HOST_KEY);
        }
        if (rmsManager.containsKey(LOGIN_KEY))
        {
            login = rmsManager.get(LOGIN_KEY);
        }
        if (rmsManager.containsKey(PASSWORD_KEY))
        {
            password = rmsManager.get(PASSWORD_KEY);
        }
        if (rmsManager.containsKey(SAVE_DATA_KEY))
        {
            saveLoginPass = rmsManager.get(SAVE_DATA_KEY).equals("1");
        }
        if (rmsManager.containsKey(NEW_ORDER_MELODY_KEY))
        {
            newOrderMelodyId = Integer.parseInt(rmsManager.get(NEW_ORDER_MELODY_KEY)); 
        }
        if (rmsManager.containsKey(START_ORDER_MELODY_KEY))
        {
            startOrderMelodyId = Integer.parseInt(rmsManager.get(START_ORDER_MELODY_KEY)); 
        }
        if (rmsManager.containsKey(CANCEL_ORDER_MELODY_KEY))
        {
            cancelOrderMelodyId = Integer.parseInt(rmsManager.get(CANCEL_ORDER_MELODY_KEY)); 
        }
        if (rmsManager.containsKey(CONNECTION_ERROR_MELODY_KEY))
        {
            connectionErrorMelodyId = Integer.parseInt(rmsManager.get(CONNECTION_ERROR_MELODY_KEY)); 
        }
        if (rmsManager.containsKey(MESSAGE_MELODY_KEY))
        {
            messageMelodyId = Integer.parseInt(rmsManager.get(MESSAGE_MELODY_KEY)); 
        }
        if (rmsManager.containsKey(UPDATE_TYPE_KEY))
        {
            updateType = rmsManager.get(UPDATE_TYPE_KEY);
        }
        if (rmsManager.containsKey(FONT_SIZE_KEY))
        {
            fontSize = Integer.parseInt(rmsManager.get(FONT_SIZE_KEY));
        }
    }
    
    public final void save()
    {
        rmsManager.set(SAVE_DATA_KEY, (saveLoginPass ? "1" : "0"));
        rmsManager.set(HOST_KEY, host);
        rmsManager.set(SECOND_HOST_KEY, secondHost);
        if (saveLoginPass)
        {
            rmsManager.set(LOGIN_KEY, login);
            rmsManager.set(PASSWORD_KEY, password);
        }
        else
        {
            rmsManager.remove(LOGIN_KEY);
            rmsManager.remove(PASSWORD_KEY);
        }
        rmsManager.set(NEW_ORDER_MELODY_KEY, Integer.toString(newOrderMelodyId));
        rmsManager.set(START_ORDER_MELODY_KEY, Integer.toString(startOrderMelodyId));
        rmsManager.set(CANCEL_ORDER_MELODY_KEY, Integer.toString(cancelOrderMelodyId));
        rmsManager.set(CONNECTION_ERROR_MELODY_KEY, Integer.toString(connectionErrorMelodyId));
        rmsManager.set(MESSAGE_MELODY_KEY, Integer.toString(messageMelodyId));
        
        rmsManager.set(UPDATE_TYPE_KEY, updateType);
        rmsManager.set(FONT_SIZE_KEY, Integer.toString(fontSize));
        
        rmsManager.save();
    }

    public String getLogin() 
    {
        return login;
    }

    public void setLogin(String login) 
    {
        this.login = login;
    }

    public String getPassword() 
    {
        return password;
    }

    public void setPassword(String password) 
    {
        this.password = password;
    }

    public boolean isSaveLoginPass()
    {
        return saveLoginPass;
    }

    public void setSaveLoginPass(boolean saveLoginPass) 
    {
        this.saveLoginPass = saveLoginPass;
    }

    public String getHost()
    {
        //return host;
         return DEFAULT_HOST;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public String getSecondHost() 
    {
        //return secondHost;
        return DEFAULT_SECOND_HOST;
    }

    public void setSecondHost(String secondHost) 
    {
        this.secondHost = secondHost;
    }

    public int getFunctionsMask() 
    {
        return functionsMask;
    }

    public void setFunctionsMask(int functionsMask) 
    {
        this.functionsMask = functionsMask;
    }
    
    public void setNewOrderMelodyId(int newOrderMelodyId)
    {
        this.newOrderMelodyId = newOrderMelodyId;
    }
    
    public int getNewOrderMelodyId()
    {
        return newOrderMelodyId;
    }

    public int getConnectionErrorMelodyId() 
    {
        return connectionErrorMelodyId;
    }

    public void setConnectionErrorMelodyId(int connectionErrorMelodyId) 
    {
        this.connectionErrorMelodyId = connectionErrorMelodyId;
    }

    public int getMessageMelodyId() 
    {
        return messageMelodyId;
    }

    public void setMessageMelodyId(int messageMelodyId) 
    {
        this.messageMelodyId = messageMelodyId;
    }

    public int getCancelOrderMelodyId() 
    {
        return cancelOrderMelodyId;
    }

    public void setCancelOrderMelodyId(int cancelOrderMelodyId) 
    {
        this.cancelOrderMelodyId = cancelOrderMelodyId;
    }

    public int getStartOrderMelodyId() 
    {
        return startOrderMelodyId;
    }

    public void setStartOrderMelodyId(int startOrderMelodyId) 
    {
        this.startOrderMelodyId = startOrderMelodyId;
    }

    public String getUpdateType() 
    {
        return updateType;
    }

    public void setUpdateType(String updateType) 
    {
        this.updateType = updateType;
    }

    public int getFontSize() 
    {
        return fontSize;
    }

    public void setFontSize(int fontSize) 
    {
        this.fontSize = fontSize;
    }    
}
